package com.aube.plugin.draw;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.widget.TextView;

import com.aube.plugin.origin.R;
import com.huyn.baseframework.dynamicload.DLLayout;
import com.huyn.baseframework.dynamicload.IPositionManager;

/**
 * Created by huyaonan on 16/8/12.
 */
public class DrawLayout extends DLLayout {

    private Paint mPaint;

    private TextView mText;

    private static final int COLOR_INVALID = 0xff666666;
    private static final int COLOR_VALID = 0xffcc0000;
    private int padding = 0;
    private boolean mValid = false;

    public DrawLayout(Context context) {
        this(context, null);
    }

    public DrawLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DrawLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void initViews() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);

        mText = new TextView(getContext());
        mText.setTextSize(14);
        mText.setTextColor(Color.WHITE);
        addView(mText);

        padding = getResources().getDimensionPixelSize(R.dimen.text_padding);
    }

    @Override
    public void fillData(Object o) {

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if(mText != null) {
            int height = 0, width=0;

            mText.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
            width += mText.getMeasuredWidth() + 2*padding;
            height += mText.getMeasuredHeight() +2*padding;

            setMeasuredDimension(width, height);
        } else {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        int height = getHeight();
        int mOffsetX = padding;
        mText.layout(mOffsetX, (height-mText.getMeasuredHeight())/2-2, mOffsetX+mText.getMeasuredWidth(), (height+mText.getMeasuredHeight())/2-2);
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        int width = getWidth();
        int height = getHeight();

        mPaint.setColor(mValid ? COLOR_VALID : COLOR_INVALID);
        RectF rectF = new RectF(0, 0, width, height);
        canvas.drawRoundRect(rectF, padding/2, padding/2, mPaint);
        super.dispatchDraw(canvas);
    }

    @Override
    public void notifyAnim(int i) {

    }

    @Override
    public void registToManager(IPositionManager iPositionManager) {

    }

    @Override
    public void unreistFromManager(IPositionManager iPositionManager) {

    }

    @Override
    public int[] getLocations() {
        return new int[0];
    }

    public void update(String text, boolean valid) {
        mText.setText(text);
        this.mValid = valid;
        invalidate();
    }

}
